package com.cytanaa.youlike;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

public class FragmentRecommendedList extends Activity implements
		ActionBar.TabListener {
	private static final String fb_id = "416965195026704";
	private SharedPreferences mPrefs;
	private Facebook facebook = new Facebook(fb_id);
	private RecommendedFragmentAdapter fun = null;
	private RecommendedFragmentAdapter movie = null;
	private RecommendedFragmentAdapter standup = null;
	private RecommendedFragmentAdapter music = null;
	public static final String likeLink2 = "http://www.facebook.com/YouLikePage";
	public static final String photoLink = "http://www.freeimagehosting.net/t/fi8ps.jpg";
	
	public static int page = 0;
	public static String[] category = new String[4];

	private static enum Tabs {
		TAB_FUN, TAB_MOVIE, TAB_STANDUP, TAB_MUSIC;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		LayoutInflater inflator = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflator.inflate(R.layout.recommended_title, null);
		this.getActionBar().setDisplayShowCustomEnabled(true);
		this.getActionBar().setDisplayShowTitleEnabled(false);
		//this.getActionBar().setDisplayUseLogoEnabled(true);
		//this.getActionBar().setDisplayShowHomeEnabled(false);

		this.getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffff0000")));

		mPrefs = getSharedPreferences("facebook_session", MODE_PRIVATE);
		String access_token = mPrefs.getString("access_token", null);
		long expires = mPrefs.getLong("access_expires", 0);
		Log.i("postToWall",
				"facebook.setAccessToken(access_token), and access_token=="
						+ access_token);
		if (access_token != null) {
			facebook.setAccessToken(access_token);
			Log.i("postToWall",
					"facebook.setAccessToken(access_token), and facebook.isSessionValid()=="
							+ facebook.isSessionValid());
		}
		if (expires != 0) {
			facebook.setAccessExpires(expires);
			Log.i("postToWall", "facebook.setAccessExpires(expires)" + expires);
		}
		this.getActionBar().setCustomView(v);

		final ActionBar bar = getActionBar();
		// titlebar_title.setText("Prefeerences");
		// titlebar_icon.setImageResource(R.drawable.titlebar_list);
		Log.v("Check point! onCreate: ", "1");
		category = getResources().getStringArray(R.array.custom_category);
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		bar.addTab(bar.newTab().setIcon(R.drawable.funny_tab)
				.setTabListener(this).setTag(Tabs.TAB_FUN));
		//bar.addTab(bar.newTab().setCustomView(v).setTabListener(this).setTag(Tabs.TAB_FUN));
		bar.addTab(bar.newTab().setIcon(R.drawable.movie_tab)
				.setTabListener(this).setTag(Tabs.TAB_MOVIE));
		bar.addTab(bar.newTab().setIcon(R.drawable.standup_tab)
				.setTabListener(this).setTag(Tabs.TAB_STANDUP));
		bar.addTab(bar.newTab().setIcon(R.drawable.music_tab)
				.setTabListener(this).setTag(Tabs.TAB_MUSIC));

	}

	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		onTabSelected(tab, ft);
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		switch ((Tabs) tab.getTag()) {
		case TAB_FUN:
			Log.v("Check point! TAB_FUN: ", "TAB_FUN");
			showFun(ft);
			break;
		case TAB_MOVIE:
			Log.v("Check point! TAB_MOVIE: ", "TAB_MOVIE");
			showMovie(ft);
			break;
		case TAB_STANDUP:
			Log.v("Check point! TAB_STANDUP: ", "TAB_STANDUP");
			showStandup(ft);
			break;
		case TAB_MUSIC:
			Log.v("Check point! TAB_MUSIC: ", "TAB_MUSIC");
			showMusic(ft);
			break;
		}
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// no-op
	}

	private void showFun(FragmentTransaction ft) {
		if (fun == null) {
			Log.v("Check point! TAB_FUN: ", "simple == null");
			fun = new RecommendedFragmentAdapter(category[0], 0);

		}

		ft.replace(android.R.id.content, fun);
	}

	private void showMovie(FragmentTransaction ft) {
		if (movie == null) {
			Log.v("Check point! showMovie: ", "movie == null");
			movie = new RecommendedFragmentAdapter(category[1], 0);
		}

		ft.replace(android.R.id.content, movie);
	}

	private void showStandup(FragmentTransaction ft) {
		if (standup == null) {
			standup = new RecommendedFragmentAdapter(category[2], 0);
		}

		ft.replace(android.R.id.content, standup);
	}

	private void showMusic(FragmentTransaction ft) {
		if (music == null) {
			music = new RecommendedFragmentAdapter(category[3], 0);
		}

		ft.replace(android.R.id.content, music);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(Menu.NONE, Menu.FIRST + 1, 5, "Setting").setIcon(
				android.R.drawable.ic_menu_preferences);
		menu.add(Menu.NONE, Menu.FIRST + 2, 5, "Like us on Facebook").setIcon(
				R.drawable.like_menu);
		menu.add(Menu.NONE, Menu.FIRST + 3, 5, "Share the fun").setIcon(
				R.drawable.share_menu);

		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent i = new Intent(this, Preferences.class);
			startActivityForResult(i, 1);

			break;
		case Menu.FIRST + 2:
			// Intent i = new Intent(this, Preferences.class);
			Intent browserIntent2 = new Intent(Intent.ACTION_VIEW,
					Uri.parse(likeLink2));
			startActivity(browserIntent2);
			break;

		case Menu.FIRST + 3:
			// Intent i = new Intent(this, Preferences.class);
			postToWall();
			break;
		}
		return false;
	}
	/**
	 * Facebook sharing handler
	 */
	public void postToWall() {
		Bundle parameters = new Bundle();

		parameters.putString("message", "");
		parameters.putString("link", likeLink2);
		parameters.putString("description", "Good Stuff!!!!");
		parameters.putString("picture", photoLink);

		if (facebook.isSessionValid()) {
			Log.i("postToWall", "mFacebook.isSessionValid()");
			// mAsyncRunner.request("me/feed", parameters, "POST", new
			// FacebookPostListener(), null);

			facebook.dialog(this, "feed", parameters, new DialogListener() {
				@Override
				public void onFacebookError(FacebookError e) {
					Log.i("onFacebookError", "youtubeLink == " + e);

				}

				@Override
				public void onError(DialogError e) {
					Log.i("onFacebookError", "onCancel == " + e);

				}

				@Override
				public void onComplete(Bundle values) {
				}

				@Override
				public void onCancel() {

				}
			});

		} else {
			Log.i("postToWall", "mFacebook.isSessionValid() is not true");
			Log.i("postToWall",
					"mFacebook.getAccessToken(): " + facebook.getAccessToken());

			facebook.authorize(this,
					new String[] { "email", "publish_stream" },
					new DialogListener() {

						@Override
						public void onCancel() {
							// Function to handle cancel event
						}

						@Override
						public void onComplete(Bundle values) {
							// Function to handle complete event
							// Edit Preferences and update facebook acess_token
							SharedPreferences.Editor editor = mPrefs.edit();
							// mFacebook.setAccessToken("tooken");
							editor.putString("access_token",
									facebook.getAccessToken());
							editor.putLong("access_expires",
									facebook.getAccessExpires());
							editor.commit();
							Log.i("postToWall", "mFacebook.getAccessToken(): "
									+ facebook.getAccessToken());
							Log.i("postToWall",
									"mFacebook.getAccessExpires(): "
											+ facebook.getAccessExpires());
						}

						@Override
						public void onError(DialogError error) {
							// Function to handle error
							Log.i("postToWall", "mFacebook.onError(): " + error);

						}

						@Override
						public void onFacebookError(FacebookError fberror) {
							// Function to handle Facebook errors
							Log.i("postToWall", "mFacebook.onFacebookError(): "
									+ fberror);
						}

					});
		}
	}
	
}
